<template>
	<view class="wrap">
		<view class="content">
			<view class="title">欢迎登录图书商城</view>
			<view class="mailbox">
				<view class="iconfont icon-icon-test33"></view>
				<input
					type="text"
					v-model="email"
					placeholder="请输入邮箱号"
				/>
			</view>
			<view class="mailbox">
				<view class="iconfont icon-icon-test26"></view>
				<input
					v-model="password"
					placeholder="请输入密码"
					type="password"
				/>
				<!-- <input style="border: 0;padding: 0;outline:none;
			background-color: transparent;" 
			:type="hidePwd?'text':'password'" v-model="password" placeholder="请输入密码" />
			<view  @tap="hidePwd=!hidePwd" class="iconfont " :class="hidePwd ?'icon-icon-test' :'icon-icon-test1'"></view>
		 -->
			</view>
			<button @tap="handleLogin" class="getCaptcha">登录</button>
			<view class="alternative">
				<view class="password">忘记密码</view>
				<view class="issue" @tap="$u.route('pages/auth/register')">
					注册
				</view>
			</view>
		</view>
		<!-- <view class="buttom">
			<view class="loginType">
				<view class="wechat item">
					<view class="icon">
						<u-icon size="70" name="weixin-fill" color="rgb(83,194,64)"></u-icon>
					</view>
					微信
				</view>
				<view class="QQ item">
					<view class="icon">
						<u-icon size="70" name="qq-fill" color="rgb(17,183,233)"></u-icon>
					</view>
					QQ
				</view>
			</view>
			<view class="hint">登录代表同意
				<text class="link">图书商城用户协议、隐私政策，</text>
				并授权使用您的图书商城账号信息（如昵称、头像、收获地址）
			</view>
		</view> -->
	</view>
</template>

<script>
export default {
	name: 'Login',
	data() {
		return {
			email: '123456@qq.com',
			password: '123456',
			hidePwd: false,
			// tabBar菜单URL列表
			tabBarUrlList: [
				'pages/index/index',
				'pages/goods/goods',
				'pages/cart/cart',
				'pages/mine/mine'
			],
			rules: {
				// usertel:{
				// rule:  /^[1](([3][0-9])|([4][0,1,4-9])|([5][0-3,5-9])|([6][2,5,6,7])|([7][0-8])|([8][0-9])|([9][0-3,5-9]))[0-9]{8}$/,
				// 	msg:"手机号码输入不规范"
				// },
				// userName:{
				//   rule : /^[a-zA-Z0-9_-]{4,16}$/,
				//   msg:"4到16位（字母，数字，下划线，减号)"
				// },
				email: {
					rule: /^\w+@[a-z0-9]+\.[a-z]+$/i,
					msg: '邮箱格式不对'
				},
				password: {
					rule: /^[0-9a-zA-Z]{6,16}$/,
					msg: '密码应该为6-16位'
				}
			}
		};
	},
	methods: {
		// 用户登录
		async handleLogin() {
			if (!this.validate('password')) return;
			if (!this.validate('email')) return;
			// 整理登录参数
			const params = {
				email: this.email,
				password: this.password
			};
			// 请求登录
			let res = await this.$u.api.authUserLogin(params);
			// 提示信息
			this.$u.toast('登录成功');
			// 本地存储token信息
			this.$u.vuex('vuex_token', res.access_token);
			// uni.setStorageSync('istoken',res.access_token)
			// 获取并本地缓存用户信息
			this.$u.utils.getAndSaveUserInfo();
			// 返回请求登录的来源页(添加首页防止出错)
			const originRoute =
				uni.getStorageSync('originRoute') || 'pages/index/index';
			this.$u.route({
				// 	// 如果从tabBar页码访问本页使用switchTab方式返回，如果从其他页面访问登录使用redirectTo方式返回（解决Android端不显示返回按钮问题）
				type: this.tabBarUrlList.includes(originRoute)
					? 'switchTab'
					: 'redirectTo',
				// 设置reLaunch类型(可跳转到任意页面)
				url: originRoute
			});
		},
		//判断验证是否符合要求
		validate(key) {
			let bool = true;
			if (!this.rules[key].rule.test(this[key])) {
				//提示信息
				uni.showToast({
					title: this.rules[key].msg,
					icon: 'none'
				});
				//取反
				bool = false;
				return false;
			}
			return bool;
		}
	}
};
</script>

<style lang="scss" scoped>
.mailbox {
	display: flex;
	align-items: center;
	height: 100rpx;
	padding: 20rpx 0;
	border-bottom: 1rpx solid #333;
	.iconfont {
		padding: 0 20rpx;
		font-size: 40rpx;
	}
	input {
		font-size: 30rpx;
	}
}
.wrap {
	font-size: 28rpx;
	position: absolute;
	top: 50%;
	left: 50%;
	transform: translate(-50%, -50%);
	.content {
		width: 600rpx;
		margin: 80rpx auto 0;

		.title {
			text-align: center;
			font-size: 60rpx;
			font-weight: 500;
			margin-bottom: 100rpx;
		}

		input {
			// height: 40rpx;
			// border: 0;
			// padding: 0;
			// text-align: left;
			// margin-bottom: 10rpx;
			// padding-bottom: 6rpx;
		}
		.tips {
			color: $u-type-info;
			margin-bottom: 60rpx;
			margin-top: 8rpx;
		}

		.getCaptcha {
			// background-color: rgb(253, 243, 208);
			// color: $u-tips-color;
			// border: none;

			box-shadow: 0 2rpx 2rpx rgba(100, 100, 100, 0.1);
			background: linear-gradient(to right, #3388ee, #4db9f1);
			color: #f8f8f8;
			font-size: 30rpx;
			padding: 12rpx 0;
			margin-top: 40rpx;
			&::after {
				border: none;
			}
		}

		.alternative {
			color: $u-tips-color;
			display: flex;
			justify-content: space-between;
			margin-top: 30rpx;
		}
	}

	.buttom {
		.loginType {
			display: flex;
			padding: 300rpx 150rpx 150rpx 150rpx;
			justify-content: space-between;

			.item {
				display: flex;
				flex-direction: column;
				align-items: center;
				color: $u-content-color;
				font-size: 28rpx;
			}
		}

		.hint {
			padding: 20rpx 40rpx;
			font-size: 20rpx;
			color: $u-tips-color;

			.link {
				color: $u-type-warning;
			}
		}
	}
}
</style>
